<?php $this->layout('layout')?>

<?php $this->start('style')?>
<link rel="stylesheet" href="/jQuery.Gantt/css/style.css?t=2020042815">
<style type="text/css">
.fn-gantt .leftPanel {
    width: 680px;
}

.rightPanel .fn-label {
    width: 100%;
}

.fn-gantt .leftPanel .bg-primary
, .fn-gantt .leftPanel .bg-secondary
, .fn-gantt .leftPanel .bg-danger
{
    color: #fff;
}
.fn-gantt .leftPanel .desc .bg-warning {
    color: #000;
}
.fn-gantt .leftPanel .desc a{
    color: #000;
}


</style>
<?php $this->end()?>


<?php $this->start('main')?>

<?php
if (!$leader) {
    $slted_user = $users[$authed->id];
} else {
    $slted_user = $users[$leader];
}
?>

<div class="form-row">
<div class="col-lg-12">
<form method="GET" action="/stats/preview">
<div class="form-inline">

<div class="input-group">
<select onchange="onChangePro( this.value, '#filterTags', '版本' );" name="pro" class="form-control">
<option value="0">项目</option>
<?php $this->insert('selection-users', ['data' => $pros, 'slt' => isset($pro_slt) ? $pro_slt : 0])?>
</select>
</div>

<div class="input-group ml-2">
<select name="tag" class="form-control" id="filterTags">
<option value="0">版本</option>
<?php $this->insert('selection-users', ['data' => $tags, 'slt' => isset($tag_slt) ? $tag_slt : 0])?>
</select>
<div class="input-group-append">
</div>
</div>

<div class="input-group ml-2">
<div class="input-group-prepend">
<span class="input-group-text">部门</span>
</div>
<select name="department" class="form-control" onchange="onChangeDepartment( this.value, '#update-leader' )">
<?php $this->insert('selection-users', ['data' => $departments, 'slt' => $department ? $department : $slted_user->department])?>
</select>
</div>

<div class="input-group ml-2">
<div class="input-group-prepend">
<span class="input-group-text">负责人</span>
</div>
<select name="leader" class="form-control" id="update-leader">
<option value="0">负责人</option>
<?php foreach ($users as $user): ?>
<?php if ($user->department == $slted_user->department): ?>
<option value="<?=$user->id?>" <?=$user->id == $leader ? 'selected' : ''?>><?=$user->name?></option>
<?php endif?>
<?php endforeach?>
</select>
</div>

<div class="input-group ml-2">
<div class="input-group-prepend">
<span class="input-group-text">截止日期</span>
</div>
<input value="<?=($today ? $today : '')?>" name="today" class="form-control " type="date">
</div>

<button class="btn btn-primary ml-2" type="submit">查 询</button>
<input <?=$only_dep ? 'checked' : ''?> class="form-check-input ml-2" type="checkbox" value="1" name="only_dep">仅看部门


</div>
</form>

</div>
</div>

<hr>

<div class="gantt"></div>


<?php $this->end()?>

<?php $this->start('script')?>

<script type="text/javascript">
var tags = <?=json_encode($tags)?>;
var users = <?=json_encode($users)?>;
</script>

<?php
$data      = array();
$t         = time();
$prioritys = [
    10  => 'bg-secondary">低',
    20  => 'bg-primary">中',
    99  => 'bg-warning">高',
    999 => 'bg-danger">急',
];
$counts = [];
foreach ($list as $task) {
    if (!isset($counts[$task->leader])) {
        $counts[$task->leader] = 0;
    }
    $counts[$task->leader]++;
}
$members = [];
foreach ($users as $user) {
    if ($slted_user->department == $user->department) {
        $members[$user->id] = true;
    }
}
foreach ($list as $key => $task) {
    if (isset($counts[$task->leader])) {
        $name = '<span class="badge bg-secondary">';
        $name .= $counts[$task->leader];
        $name .= '</span>';
        $name .= $users[$task->leader]->name;
        unset($counts[$task->leader]);
        unset($members[$task->leader]);
    } else {
        $name = '';
    }

    $customClass = 'ganttOrange';
    if ($task->status >= 30) {
        $customClass = 'ganttGreen';
    }
    if (!$task->deadline) {
        $customClass = 'ganttRed';
    }

    $desc = '<span class="badge ';
    $desc .= $prioritys[$task->priority];
    $desc .= '</span> ';
    $desc .= "<a href='/task/show/{$task->id}' target='_blank'>#";
    $desc .= $task->id;
    $desc .= ' ';
    $desc .= $task->title;
    $desc .= '</a>';

    $deadline = $task->deadline > 0 ? $task->deadline : $t;
    $from     = $task->ks > 0 ? $task->ks * 1000 : $deadline * 1000;
    $to       = $deadline * 1000;
    $listan   = [
        'name'   => $name,
        'desc'   => $desc,
        'values' => [
            [
                'from'        => $from,
                'to'          => $to,
                'label'       => $task->jd . '%',
                'customClass' => $customClass,
            ],
        ],
    ];

    $data[] = $listan;
}
if ($only_dep) {
    foreach ($members as $id => $value) {
        $name = '<span class="badge bg-secondary">0</span>';
        $name .= $users[$id]->name;

        $listan = [
            'name'   => $name,
            'values' => [
                [
                    'from'        => $t * 1000,
                    'to'          => $t * 1000,
                    'label'       => '--',
                    'customClass' => 'ganttBlue',
                ],
            ],
        ];

        $data[] = $listan;
        // array_unshift($data, $listan);
    }
}
?>
<script src="/jQuery.Gantt/js/jquery.fn.gantt.js?t=2020042813"></script>
<script type="text/javascript">

$(".gantt").gantt({
    source: <?=json_encode($data)?>,
    months : ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
    dow : ["日", "一", "二", "三", "四", "五", "六"],
    itemsPerPage : 100000,
    scale: "days",
    minScale: "days",
    maxScale:"months",
    navigate: "scroll",
    waitText: "加载中...",
    scrollToToday: true,
    onRender: function() {
        $("#spacer").html("任务总数：" + <?=count($list)?>)
    }
});



setTimeout( "location.reload()", 1000 * 60 * 5 );

</script>

<?php $this->end()?>
